<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {

/*用户注册*/
    public function login()
    {
        $this->display();
    }
    public function regist()
    {     
    $user = M("users");

    var_dump(trim(I('post.uphonenumber')));
        if ($user->create()) {
            if ($_FILES['file']['error'] == 0) {
                $logoinfo = $this->upload();
                $pname = $logoinfo['file']['savename'];
            } else{
                $pname = "default.jpg";
            } 

            $user->uname=trim(I('post.uname'));
            $upassword=trim(I('post.upassword'));
            $user->upassword=md5($upassword);
            $user->uadress=trim(I('post.uadress'));
            $user->uphonenumber=trim(I('post.uphonenumber'));
            $user->uphoto=$pname;
          
            if ($user->add()) {
                $this->success('注册成功！', 'reg');
            } else {$this->error('数据错误'); 
            var_dump($user->uname);
             var_dump($user->uphonenumber);
                      }
        } else {
             var_dump($user->uname);
             var_dump($user->uphonenumber);
            $this->error($user->getError());
        }
    }
    public function upload()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =  './';
        $upload->rootPath = 'Public/Uploads/photo/'; // 设置附件上传根目录
        //$upload->savePath  =     ''; // 设置附件上传（子）目录
        $upload->autoSub = false;
        $info = $upload->upload();  // 上传文件
       
        if (!$info) {
            $this->error($upload->getError());
        } else {
            return $info;
        }
    }

    public function userlist(){
        $userModel=M('users');
        $users=$userModel->order('uid')->select();
        $this->users=$users;
        $this->display();
    }
    public function delUser($id){
	$usersmodel=M('users');	
	if (false!==$usersmodel->where("uid=$id")->delete()) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败');
        }
		
	}

	public function getuserbyid($id){
	$usersmodel=M('users');	
	$users=$usersmodel->where("uid=$id")->find();
	$this->users=$users;
	$this->display();
	}

	     public function update(){
        $data=$_POST;
        // var_dump($data);
        $uid=$data['uid'];
        // var_dump($uid);
    $usersModel=M('users');
    $createresult=$usersModel->create();
    // var_dump($createresult);
     $result=$usersModel->where("uid=$uid")->save($data);
     // var_dump($result);
        if ($result) {
            $this->success('修改成功','userlist');
        }else{
            $this->error('修改失败');
        }

   }
public function checkLogin(){
        // $code=I('post.verify');  //获取用户在表单中输入的验证码字符串
        // if(!$this->check_verify($code))$this->error('验证码错误'); //调用check_verify函数，将用户输入的验证码加密处理后和Session中已存的验证码进行比较

        $usermodel=M('users'); //实例化diuba_users表
        $map   =   array();
        $map['uname']   =trim(I('post.uname'));//获取用户输入的用户名
        $map['upassword']    =md5((I('post.upassword')));//获取用户输入的密码，并用md5加密
       $contents = $_POST['contents'];
       echo $contents  ;
        $res=$usermodel->where($map)->select();
        if($res==null){
            $this->error('账号或密码错误');
        }else{
        // $_SESSION['telephone']=$map['telephone'];
        // $_SESSION['alias']=$res[0]["aliasname"]; 
            if( $contents==2){
                redirect(U("User/userlist"));
            }else{
              redirect(U("Admin/adminlist"));
            }
        }
        }
//     public function verify(){
//         ob_clean();//清空缓存
//         $Verify = new \Think\Verify();//调用验证码类实例化对象
//         $Verify->entry();//输出验证码图片，并将验证码加密处理后放到Session中
//     }
//     // 检测输入的验证码是否正确，$code为用户输入的验证码字符串
//    function check_verify($code, $id = ''){
//     $verify = new \Think\Verify();
//     return $verify->check($code, $id);
// }
        
}